﻿namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Text;
    using System.Web;
    using System.Web.Mvc;
    using Moq;
    using Xunit;

    public sealed class TextResultFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<TextResult>()
                .DerivesFrom<ContentResult>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new TextResult());
        }

        [Fact]
        public void ctor_object()
        {
            Assert.NotNull(new TextResult("test"));
        }

        [Fact]
        public void ctor_objectNull()
        {
            Assert.NotNull(new TextResult(null as object));
        }

        [Fact]
        public void prop_ContentEncoding_get()
        {
            Assert.Equal<Encoding>(Encoding.UTF8, new TextResult().ContentEncoding);
        }

        [Fact]
        public void prop_ContentType_get()
        {
            Assert.Equal<string>("text/plain", new TextResult().ContentType);
        }

        [Fact]
        public void op_ExecuteResult_ControllerContextNull()
        {
            Assert.Throws<ArgumentNullException>(() => new NotAcceptableResult().ExecuteResult(null as ControllerContext));
        }

        [Fact]
        public void op_ExecuteResult_ControllerContext()
        {
            var mock = new Mock<HttpContextBase>();
            mock.SetupSet(x => x.Response.ContentEncoding).Callback(expected => Assert.Equal<Encoding>(Encoding.UTF8, expected)).Verifiable();
            mock.SetupSet(x => x.Response.ContentType).Callback(expected => Assert.Equal<string>("text/plain", expected)).Verifiable();
            mock.Setup(x => x.Response.Write(It.IsAny<string>())).Verifiable();

            new TextResult("test").ExecuteResult(new ControllerContext()
            {
                Controller = new TestController<string>(),
                HttpContext = mock.Object
            });

            mock.VerifyAll();
        }
    }
}